<template>
  <el-dialog :visible="showDialog" :title="form.id? '编辑部门' : '添加子部门'" @close="close">
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" size="mini" placeholder="2-10个字符" style="width:80%" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" size="mini" placeholder="2-10个字符" style="width:80%" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="form.managerId" size="mini" placeholder="请选择负责人" style="width:80%">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          /></el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" size="mini" placeholder="2-100个字符" style="width:80%" :rows="4" />
      </el-form-item>
      <el-form-item>
        <el-row style="width:80%" justify="center" type="flex">
          <el-button type="success" plain @click="query">确认</el-button>
          <el-button type="danger" plain @click="close">取消</el-button>
        </el-row>
      </el-form-item>
    </el-form>

  </el-dialog>
</template>

<script >
import { editDepartment, getDepartmentList, getManager, queryDepartment, editDepartmentApi } from '@/api/department'
export default {
  props: ['showDialog', 'parentId'],
  data() {
    return {
      form: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称2-10个字符', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            let list = await getDepartmentList()
            if (this.form.id) {
              list = list.filter(item => item.id !== this.form.id)
            }
            list.some(item => item.name === value) ? callback(new Error('部门名称已存在')) : callback()
          }, trigger: 'blur' }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码2-10个字符', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            let list = await getDepartmentList()
            if (this.form.id) {
              list = list.filter(item => item.id !== this.form.id)
            }
            list.some(item => item.code === value) ? callback(new Error('部门编码已存在')) : callback()
          }, trigger: 'blur' }],
        managerId: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 2, max: 100, message: '部门介绍2-100个字符', trigger: 'blur' }]
      },
      managerList: []
    }
  },
  created() {
    this.manager()
  },
  methods: {
    async edit() {
      this.form = await editDepartment(this.parentId)
    },
    close() {
      delete this.form.id
      this.$emit('update:show-dialog', false)
      this.$refs.form.resetFields()
    },
    async manager() {
      this.managerList = await getManager()
    },
    query() {
      this.$refs.form.validate(async ok => {
        if (ok) {
          if (this.form.id) {
            await editDepartmentApi(this.form)
            this.$message.success('修改部门成功')
          } else {
            this.form.pid = this.parentId
            await queryDepartment(this.form)
            this.$message.success('增添子部门成功')
          }
          this.$emit('addDeparent')
          this.close()
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped>

</style>
